﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using InternRegistration.Model;
using InternRegistration.Common;
using System.Collections.ObjectModel;
using InternRegistration.View;
using System.Windows.Media.Imaging;
using InternRegistration.DataBase;
/**************************************************************************************
 * This is the ViewModel of SearchEntryView. It's designed to make searches           *
 * in the database and open different versions of DisplayView to make registrations,  *
 * ,editing or displaying.                                                            *
 * By: Efe Gürkan                                                                     *
 **************************************************************************************/
namespace InternRegistration.ViewModel
{
    class SearchEntryViewModel : ViewModelBase
    {

        
        #region Properties

        //Search parameters will be held nn Intern "search"
        private Intern search;

        //Selected Intern from Listview is held on "Selected"
        private Intern _Selected;
        public Intern Selected
        {
            get { return _Selected; }
            set { _Selected = value; OnPropertyChanged("Selected"); }

        }

        //Search results will be held on SearchList
        private ObservableCollection<Intern> searchList;
        public ObservableCollection<Intern> SearchList
        {
            get { return searchList; }
            set { searchList = value; OnPropertyChanged("SearchList"); }
        }

        

        //To check if any search has done,
        private bool searched;
        public bool Searched
        {
            get { return searched; }
            set { searched = value; OnPropertyChanged("Searched"); }
        }

        //search's properties assigned!
        public long SearchID
        {
            get { return search.Id; }
            set { search.Id = value; OnPropertyChanged("SearchID"); }
        }

        public string SearchName
        {
            get { return search.Name; }
            set { search.Name = value; OnPropertyChanged("SearchName"); }
        }

        public string SearchSurname
        {
            get { return search.Surname; }
            set { search.Surname = value; OnPropertyChanged("SearchSurname"); }
        }

        public string SearchSupervisor
        {
            get { return search.Supervisor; }
            set { search.Supervisor = value; OnPropertyChanged("SearchSupervisor"); }
        }


        public DateTime StartDate
        {
            get { return search.InternshipStarting; }
            set { search.InternshipStarting = value; OnPropertyChanged("StartDate"); }
        }

        public string SearchUni
        {
            get { return search.School; }
            set { search.School = value; OnPropertyChanged("SearchUni"); }
        }

        public string SearchDepartment
        {
            get { return search.Department; }
            set { search.Department = value; OnPropertyChanged("SearchDepartment"); }
        }

        public double SearchGPA
        {
            get { return search.GradePointAverage; }
            set { search.GradePointAverage = value; OnPropertyChanged("SearchGPA"); }
        }

        public string SearchPL
        {
            get { return search.ProgrammingLanguages; }
            set { search.ProgrammingLanguages = value; OnPropertyChanged("SearchPL"); }
        }



        #endregion

        /// <summary>
        /// Default constructor for SearchEntryViewModel.
        /// Search for all interns and sends it to the listview
        /// </summary>
        public SearchEntryViewModel()
        {   
            //Create default Intern
            search = new Intern();

            //Set Datepicker to minvalue, 
            search.InternshipStarting = DateTime.MinValue;

            //Singleton default seach with DBCOntrol
            SearchList = DBControl.Instance.Search(null);
            //Update list.
            OnPropertyChanged("SearchList");
            StartDate = DateTime.Today;
        }

        #region Commands

        
        #region CloseWindowCommand
        /// <summary>
        /// When Close button "X" is pressed, it fires.
        /// </summary>
        private RelayCommand closeWindowCommand;

        public RelayCommand CloseWindowCommand
        {
            get
            {
                if (closeWindowCommand == null)
                {
                    closeWindowCommand = new RelayCommand(param => this.CloseWindow(),  p => this.CanCloseWindow());
                }
                return closeWindowCommand;
            }
        }

        /// <summary>
        /// Releases the connection before exit.
        /// </summary>
        private void CloseWindow()
        {   
            //Release connection before closing.
            DBControl.Instance.Release();
        }

        //Check if method is usable 
        private bool CanCloseWindow()
        {
            return true;
        }


        #endregion

        #region SearchCommand
        /// <summary>
        /// Search Button's Command
        /// </summary>
        /// <seealso cref="DBControl.Search()"/>
        void Search()
        {   
            //Singleton Search with Intern variable (not default search)
            SearchList = DBControl.Instance.Search(search);
            OnPropertyChanged("SearchList"); // update list.
            Searched = true; // Cause Something has been searched
        }

        //Check if it's usable.
        bool CanSearch()
        {
            return true;
        }

        //Instanciating the Command
        RelayCommand _SearchCommand;
        public RelayCommand SearchCommand
        {
            get
            {
                if (_SearchCommand == null)
                {
                    _SearchCommand = new RelayCommand(p => this.Search(),
                        p => this.CanSearch());
                }
                return _SearchCommand;
            }
        }

        #endregion

        #region NewCommand
        /// <summary>
        /// To ropen the registration window, fires when "Yeni Kayıt" is pushed.
        /// </summary>
        void NewRegister()
        {   
            //Creates a new window
            Window register = new DisplayView();
            //Adding ViewModel to the window
            register.DataContext = new DisplayViewModel();
            //Opens it as a ShowDialog, so users can't switch between windows while it's opened.
            register.ShowDialog();

        }

        //Checking...
        bool CanNew()
        {
            return true;
        }
        //Instanciating...
        RelayCommand _NewCommand;
        public RelayCommand NewCommand
        {
            get
            {
                if (_NewCommand == null)
                {
                    _NewCommand = new RelayCommand(p => this.NewRegister(),
                        p => this.CanNew());
                }
                return _NewCommand;
            }
        }
        #endregion
        
        #region LeftDoubleClick
        /// <summary>
        /// Fires when an item in ListView is selected, opens the selected item in DisplayView
        /// </summary>
        void OpenProfile()
        {   //Unless selected is null...
            if (Selected != null)
            {
                Window register = new DisplayView();  //Instanciating
                register.DataContext = new DisplayViewModel(Selected, false); //Adding VM
                register.ShowDialog(); // Open as dialog so user can't switch between windows while it's open
            }
        }

        //Checking...
        bool CanOpen()
        {
            return true;
        }

        //Instanciating
        RelayCommand _LeftDoubleClick;
        public RelayCommand LeftDoubleClick
        {
            get
            {
                if (_LeftDoubleClick == null)
                {
                    _LeftDoubleClick = new RelayCommand(p => this.OpenProfile(),
                        p => this.CanOpen());
                }
                return _LeftDoubleClick;
            }
        }

        #endregion

        #region Clear
        /// <summary>
        /// Clears search inputs and dafualts the listview
        /// </summary>
        void Clear()
        {
            //Default inputs
            StartDate = DateTime.Today;
            SearchID = 0;
            SearchName = "";
            SearchSurname = "";
            SearchSupervisor = "";
            SearchUni = "";
            SearchDepartment = "";
            SearchGPA = 0;
            StartDate = DateTime.MinValue;
            //Default List
            SearchList = DBControl.Instance.Search(null);
            //Defaulted, So  there is no search has done 
            Searched = false;

        }

        //Checking
        bool CanClear() { return Searched; }

        //Instancitaing.
        RelayCommand _ClearCommand;
        public RelayCommand ClearCommand
        {
            get
            {
                if (_ClearCommand == null)
                {
                    _ClearCommand = new RelayCommand(p => this.Clear(),
                        p => this.CanClear());
                }
                return _ClearCommand;
            }
        }
        #endregion
        #endregion
    }
}
